//--------------------------------------------------------------------
//     此代码由FastEasy框架自动生成
//	   生成时间 2023-10-07 19:43:49
//     对此文件的更改可能会导致不正确的行为，并且如果重新生成代码，这些更改将会丢失。
//--------------------------------------------------------------------
using FastEasy.Model.RequestDTO;
using FastEasy.Model.ResultDTO;
using Microsoft.AspNetCore.Authorization;
using System.Text.Json;

namespace FastEasyAPI.Controllers
{
    /// <summary>
    /// UserInfo控制器接口
    /// </summary>
    [Route("[controller]")]
    [Authorize]
    [ApiController]
    public class UserInfoController : ControllerBase
    {
        private readonly IUserInfoService service;

        public UserInfoController(IUserInfoService _service)
        {
            service = _service;
        }

        /// <summary>
        /// 注册方法
        /// </summary>
        /// <param name="signDto">实体</param>
        /// <returns></returns>
        [HttpPost]
        [FastEasyRoute(SwaggerVersion.FastEasy, "Sign")]
        public async Task<ResultDto> Sign([FromBody] SignDto signDto)
        {
            ResultDto result = new() { State = false };
            UserInfo userInfo = new()
            {
                CreateDate = DateTime.Now,
                Email = signDto.Name,
                PassWord = signDto.Pass,
                UpdateDate = DateTime.Now,
                LastLoginDate = DateTime.Now,
                UserState = 1,
                Name = "火星儿童",
                Phone = "123",
                UserName = "FastEasy用户"
            };
            var state = await service.CreateEntityAsync(userInfo);
            result.State = state;
            result.Message = state ? "注册成功" : "注册失败";
            return result;
        }

        /// <summary>
        /// 登录方法
        /// </summary>
        /// <param name="signDto">实体</param>
        /// <returns></returns>
        [HttpPost]
        [FastEasyRoute(SwaggerVersion.FastEasy, "Login")]
        public async Task<ResultDto> Login([FromBody] SignDto signDto)
        {
            ResultDto result = new() { State = false };

            var entity = await service.GetEntitysByWhereAsync(a => a.Email == signDto.Name && a.PassWord == signDto.Pass);
            if (entity.Count > 0)
            {
                result.State = true;
                result.Data = JsonSerializer.Serialize(new UserInfoDto
                {
                    Avatar = entity[0].Avatar,
                    Email = signDto.Name,
                    UserId = entity[0].UserID,
                    UserName = entity[0].UserName,
                });
            }
            return result;
        }
    }
}